@extends('admin.home.main')

@section('body')
<section id="main-content">
	  <section class="wrapper">
		<h3>课程分类添加</h3>
		<!-- BASIC FORM ELELEMNTS -->
		<div class="row mt">
		  <div class="col-lg-12">
			<div class="form-panel">
			  <form class="form-horizontal style-form" action="/admin/course/insert" method="post">
			  	<input type="hidden" id="_token" name="_token" value="{{csrf_token()}}">
			  	<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">课程分类名称</label>
				  <div class="col-sm-10">
					<select class="form-control" id="main_tid" name="main_tid" style="width:150px;float:left;">
	               		<option value="0">请选择</option>
	               		@foreach($ctlist as $ctm=>$cst)
	               			<option value="{{$cst->id}}">{{$cst->type_name}}</option>
	               		@endforeach
	                </select>
	                <select class="form-control" id="tid" name="tid" style="width:150px;float:left; margin-left:5px;" disabled="disabled">
	               		<option value="0">请选择</option>
	                </select>
				  </div>
				</div>

				<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">课程名称</label>
				  <div class="col-sm-10">
					<input type="text" style="width:400px;" class="form-control" id="name" name="name" placeholder="请输入课程名称" size="50">
				  </div>
				</div>

				<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">课程简介</label>
				  <div class="col-sm-10">
					<input type="text" style="width:400px;" class="form-control" id="desc" name="desc" placeholder="请输入课程简介" size="50">
				  </div>
				</div>

				<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">课程价格</label>
				  <div class="col-sm-10">
					<input type="text" style="width:400px;" class="form-control" id="price" name="price" placeholder="请输入课程价格" size="50">
				  </div>
				</div>

				<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">课程集数</label>
				  <div class="col-sm-10">
					<input type="text" style="width:400px;" class="form-control" id="videonum" name="videonum" placeholder="请输入课程集数" size="50">
				  </div>
				</div>

				<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">课程标签</label>
				  <div class="col-sm-10">
				  	@foreach($taglist as $atm=>$ast)
						<input type="checkbox" class="tag" name="tag" value="{{$ast->id}}"> {{$ast->tag_name}}&nbsp;&nbsp;
					@endforeach
				  </div>
				</div>

				<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">课程总数量</label>
				  <div class="col-sm-10">
					<input type="text" style="width:400px;" class="form-control" id="coursenum" name="coursenum" placeholder="请输入课程总数量" size="50">
				  </div>
				</div>

				<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">是否免费</label>
				  <div class="col-sm-10">
					<input type="radio" class="isfree" name="isfree" value="1"> 不免费&nbsp;&nbsp;
					<input type="radio" class="isfree free" name="isfree" value="0"> 免费&nbsp;&nbsp;
					<input type="radio" class="isfree" name="isfree" value="2"> 部分免费&nbsp;&nbsp;
				  </div>
				</div>
				
				<div class="form-group">
				  <label class="col-sm-2 col-sm-2 control-label">单集购买</label>
				  <div class="col-sm-10">
					<input type="radio" class="isbuy" name="isbuy" value="1"> 支持&nbsp;&nbsp;
					<input type="radio" class="isbuy" name="isbuy" value="0"> 不支持&nbsp;&nbsp;
				  </div>
				</div>

				<button type="button" id="save" class="btn btn-success">提交</button>
				<button type="reset" class="btn btn-primary">重置</button>
			  </form>
			</div>
		  </div>
		  <!-- col-lg-12-->
		</div>

		
	  </section>
	  <!-- /wrapper -->
	</section>
<script type="text/javascript">
	$('#main_tid').change(function(){
		var main_tid = $('#main_tid').val();
		if(!main_tid){
			alert('请选择课程主栏目');
			return false;
		}

		$.ajax({
			url:'/admin/common/getcoursetypelist/'+main_tid,
			type:'get',
			dataType:'json',
			success:function(data){
				if(data.code == 200){
					var _data = data.list.data;
					var _html = '<option value="0">请选择</option>';
					for(var i=0; i < _data.length; i++){
						_html += '<option value="'+_data[i].id+'">'+_data[i].type_name+'</option>';
					}
					$('#tid').html(_html);
					$('#tid').removeAttr('disabled');
				}
			}
		})
	})

	// $('.free').click(function(){
	// 	var free = $('.free').val();
	// 	console.log(free);
	// 	$('.isbuy').attr('disabled','disabled');
	// })


	// if($('.free').is(':checked')){
	// 	$('.isbuy').attr('disabled','disabled');
	// }else{
	// 	$('.isbuy').removeAttr('disabled');
	// }
	var free = '';
	$('.isfree').click(function(){
		free = $(this).val();
		if(free == 0){
			$('.isbuy').attr('disabled','disabled');
		}else{
			$('.isbuy').removeAttr('disabled');
		}
	})



	$('#save').click(function(){
		var _token = $('#_token').val(),
			name = $('#name').val(),
			main_tid = $('#main_tid').val(),
			tid = $('#tid').val(),
			desc = $('#desc').val(),
			price = $('#price').val(),
			videonum = $('#videonum').val(),
			coursenum = $('#coursenum').val(),
			tag = [],
			isfree = '',
			isbuy = '';
		
		$('.tag').each(function(){
			if($(this).is(':checked')){
				tag.push($(this).val());
			}
		})

		var _tag = tag.join(',');

		$('.isbuy').each(function(){
			if($(this).is(':checked')){
				isbuy = $(this).val();
			}
		})

		$('.isfree').each(function(){
			if($(this).is(':checked')){
				isfree = $(this).val();
			}
		})

		if(!main_tid){
			alert('请选择课程主分类');
			return false;
		}

		if(!tid){
			alert('请选择课程分类');
			return false;
		}

		if(!name){
			alert('请输入课程名称');
			return false;
		}

		if(!desc){
			alert('请输入课程简介');
			return false;
		}

		if(!price){
			alert('请输入课程价格');
			return false;
		}

		if(!videonum){
			alert('请输入课程集数');
			return false;
		}

		if(!_tag){
			alert('请选择课程标签');
			return false;
		}

		if(!isfree){
			alert('请选择是否免费');
			return false;
		}

		if(isfree > 0 && !isbuy){
			alert('请选择是否支持单集购买');
			return false;
		}

		$.ajax({
			url:'/admin/course/insert',
			type:'post',
			data:{'_token':_token,'name':name,'tmid':main_tid,'tid':tid,'desc':desc,'price':price,'tag':_tag,'isbuy':isbuy,'videonum':videonum,'coursenum':coursenum,'is_free':isfree},
			dataType:'json',
			success:function(msg){
				if(msg.code == 200){
					alert('添加成功');
					location.href="/admin/course/edit/"+msg.course_id;
				}else if(msg.code == 300){
					alert('课程已存在,请勿重复添加');
					location.reload();
				}else{
					alert('添加失败');
					location.reload();
				}
			}
		})
	})
</script>
@endsection